import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

	
class Download extends JComponent {
	public JProgressBar pb = new JProgressBar(0, 100);
	private JButton pause, remove;
	private JPanel container;
	private JScrollPane scrollPane;
	private boolean isInterrupted = false;
	private boolean isPaused = false;
	
	Download(String uri, JPanel acontainer, JScrollPane ascrollPane) {
	   container = acontainer;
	   scrollPane = ascrollPane;
		setLayout(new FlowLayout(FlowLayout.LEADING));
		setPreferredSize(DManagerFrame.DLCOM_PREF_DIM);
		setMaximumSize(DManagerFrame.DLCOM_PREF_DIM);
		setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
		Label name = new Label(uri.trim().substring(0, uri.length() > 20 ? 20 : uri.length()));
		name.setPreferredSize(new Dimension(DManagerFrame.LABEL_PREF_WIDTH, 20));
		add(name);
		add(new JSeparator(SwingConstants.VERTICAL));
		add(pb);
		
		pause = new JButton(DManagerFrame.pauseIcon);
		remove = new JButton(DManagerFrame.removeIcon);
		pause.setPreferredSize(new Dimension(20, 20));
		remove.setPreferredSize(new Dimension(20, 20));
		add(pause);
		add(remove);
		initEventHandlers();
	}
	
	public boolean isInterrupted () { return isInterrupted;}
	public boolean isPaused () { return isPaused;}
   
	public void destroy() {
	   container.remove(Download.this);
      scrollPane.setViewportView(container);
	}
	
	private void initEventHandlers() {
	   remove.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            synchronized(Download.this) {
               destroy();
               isInterrupted = true;
               Download.this.notify();
            }
         }
      });
	   pause.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            synchronized(Download.this) {
               isPaused = !isPaused;
               Download.this.notify();
            }
         }
      });
   }
}
